<template>
  <div id="myChart" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { findTaskRunningLog } from '@/api/dashboard/app'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '96%'
    },
    height: {
      type: String,
      default: '250px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null,
      chartData: {
        legend: [],
        times: [],
        seriesList: [],
      }
    }
  },
  mounted() {
    findTaskRunningLog().then(res => {
      res.seriesList.forEach(element => {
        element.symbol = 'none'
      });
      this.chartData = res;
      this.initChart()
    })
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    setOptions() {
      var myChart = this.$echarts.init(document.getElementById('myChart'))
      myChart.setOption({
        color: ["#1CA965", "#347DE1", "#FFB975", "#e5323e"],
        xAxis: {
          data: this.chartData.times,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#131D33'
            }
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 0,
          top: 50,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: "rgba(0,0,0,0.4)",
          textStyle: {
            color: "#FFFFFF"
          },
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          name: '个数',
          type: 'value',
          splitNumber: 6,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#C8CCDD'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              width: 2,
              type: [2, 20],
              dashOffset: 5
            }
          },
          splitArea: { show: false }
        },
        legend: {
          data: this.chartData.legend
        },
        series: this.chartData.seriesList
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    }
  }
}
</script>
